@{
    Layout = "~/Views/Shared/_Dashboard.cshtml";
}
@UIHelper.ImageTitle(Html.Global("RES_USER_MGT"), "", "~/content/images/icon_user_48.png")
@{ 
    Ajax.Dna().Grid<MembershipUser>()
       .Name("AccountGrid")
        .Header(() =>
        {
            Ajax.Dna().Toolbar()
                .HtmlAttributes(new { style = "margin-bottom:1px" })
                .Name("AccountsToolbar")
                .Buttons(btns =>
                {
                    btns.AddLabel(Html.Global("RES_USER_NAME"));
                    btns.Add().Template(() =>
                    {
                        Ajax.Dna().TextBox("txtUserSearchTerm").AutoComplete(Url.Action("GetSuggests", "Account", new { Area = "" })).IconImageUrl(Url.Content("~/content/images/search_16.png")).Render();
                    });
                    btns.AddButton(Html.Global("RES_FIND"), "if ($(\"#txtUserSearchTerm\").val()) $(\"#AccountGrid\").dgrid(\"addFilter\",\"UserName\",\"eq\",$(\"#txtUserSearchTerm\").val(),true); else $(\"#AccountGrid\").dgrid(\"removeFilter\",\"UserName\");");
                    btns.AddSpliter();
                    btns.AddButton(Html.Global("RES_BAND"), "_bendUser();").ImageIcons(Url.Content("~/content/images/icon_block_16.png"));
                    btns.AddButton(Html.Global("RES_UNLOCK"), "_unlockUser();").ImageIcons(Url.Content("~/content/images/icon_accept_16.png"));
                    btns.AddButton(Html.Global("RES_SENDMAIL")).ImageIcons(Url.Content("~/content/images/mail_send_16.png")).Name("tbbtnSendMailToUser");
                    btns.AddButton(Html.Global("RES_DETAIL")).ImageIcons(Url.Content("~/content/images/contact.gif")).Name("tbbtnViewUserDetail");
                    btns.AddSpliter();
                    btns.AddButton(Html.Global("RES_HELP"), "window.open(\"" + Url.Help(2) + "\");").ImageIcons(Url.Content("~/content/images/icon_help_16.png"));
                })
                .Render();
        })
        .Options(opts =>
        {
            var scripts = new System.Text.StringBuilder();
            scripts.Append("$(\"#AccountGrid\").dgrid(\"load\",\"")
                   .Append(Url.Action("UserList", "Account", new { Area = "" }))
                   .Append("\",{filter:")
                   .Append("$(\"#AccountGrid\").dgrid(\"getFilters\")")
                   .Append(",index:1")
                   .Append(",size:$(\"#AccountGrid_pager\").pager(\"option\",\"pagesize\")")
                   .Append("},\"").Append("AccountGrid_tmpl\"")
                   .Append(",\"POST\",function(data){ $(\"#AccountGrid_pager\").pager(\"reset\", data.Total);} );");
            opts.OnFilterChanged = scripts.ToString();
        })
           .Columns(cols =>
           {
               cols.Bound(m => m.UserName).Title(Html.Global("RES_USER_NAME")).Width(300).ClientTemplate(@<text><a href="javascript:void(0)">
                   <img src="{{if !IsApproved}} @Url.Content("~/content/images/icon_block_16.png")
                            {{else}}
                              {{if IsOnline}}
                                  @Url.Content("~/content/images/icon_user_16.gif")
                              {{else}}
                                  @Url.Content("~/content/images/icon_user_off_16.gif")
                              {{/if}}
                            {{/if}}
                            " alt="${UserName}" style="float: left;" />
                   ${UserName} </a>
    <input type="hidden" class="d-user-value" value="${UserName}" />
    </text>);
               cols.Bound(m => m.Email).Title(Html.Global("RES_EMAIL")).Width(400);
               cols.Bound(m => m.CreationDate).Title(Html.Global("RES_CREATION")).Width(200).ClientTemplate(@<text>{{=
    $dna.formatDate(CreationDate)}} </text>);
               cols.Bound(m => m.LastLoginDate).Title(Html.Global("RES_LAST_LOGIN")).Width(200).ClientTemplate(@<text>{{=
    $dna.formatDate(LastLoginDate)}} </text>);
           })
        .ClientBind(Url.Action("UserList", "Account", new { Area = "" }))
        .Pagable(1, 25)
        .Render();


    Ajax.Dna().Dialog(Html.Global("ManageUser_UserDetail"), "tbbtnViewUserDetail")
        .Name("ViewUserDetailDialog")
        .Options(opts =>
        {
            opts.AutoOpen = false;
            opts.OnOpen = "$(this).dialog(\"option\",\"title\",_getSelectedUserName()); _viewUserDetailHandler();";
            opts.CssClass = "d-dialog";
            opts.Width = 570;
            opts.ShowModal = true;
            opts.IsResizable = false;
            opts.ShowEffect = jQueryEffects.Scale;
            opts.HideEffect = jQueryEffects.Drop;
        })
        .Body(@<text><div id="UserDetailViewContainer">
        </div></text>)
        .Buttons(btns =>
        {
            btns.Add(Html.Global("RES_SAVE"), "_saveUserRoles($(this));");
            btns.AddClose();
        })
       .Render();
    Ajax.Dna().Dialog(Html.Global("ManageUsers_SendMail"), "tbbtnSendMailToUser")
             .Options(opts =>
             {
                 opts.AutoOpen = false;
                 opts.CssClass = "d-dialog";
                 opts.Width = 500;
                 opts.OnOpen = "$(this).dialog(\"option\",\"title\",\"" + Html.Global("ManageUsers_SendMailTo") + " \"+_getSelectedUserName());_loadEmailWriter();";
                 opts.ShowModal = true;
                 opts.IsResizable = false;
             })
          .Body(@<text><div id="EmailWriterContainer">
          </div></text>)
       .Buttons(btns =>
       {
           btns.Add(Html.Global("RES_SEND"), "_sendMail();");
           btns.AddClose();
       })
       .Render(); 
}
<script type="text/javascript">
    function _sendMail() {
        //$("#txtEmailBody").richtextbox("syncContent", "source");
        if ($("#txtEmailText").val() && $("#txtEmailBody").val()) 
        {
           $("#EmailWriterContainer").find("form:first").submit();
        }
        else
           $("#txtEmailText").focus();
    }

    function _loadEmailWriter() {
        $.ajax({
            url: '@Url.Action("SendMail", "Account", new { Area = "" })',
            data: { username: _getSelectedUserName() },
            beforeSend: function () {
                uiHelper.blockUI($("#EmailWriterContainer"));
            },
            success: function (data) {
                $("#EmailWriterContainer").html(data);
                uiHelper.unblockUI($("#EmailWriterContainer"));
            }
        });
    }

    function _ensureUserSelection() {
        if (_getSelectedUserName())
            return true;
        else {
            alert("@Html.Global("ManageUser_SelectUserFirst")");
            return false;
        }
    }

    function _bendUser() {
        if (_ensureUserSelection()) {
            uiHelper.confirm("@Html.Global("RES_QUESTION")", "@Html.Global("ManageUser_Confirm_BindUser")", function () {
                $.post('@Url.Action("Bend", "Security", new { Area = "" })',
                    { username: _getSelectedUserName() }, function () { $("#AccountGrid_pager").pager("reload"); });
            });
        }
    }

    function _unlockUser() {
        if (_ensureUserSelection()) {
            $.post('@Url.Action("Unlock", "Security", new { Area = "" })',
                    { username: _getSelectedUserName() }, function () { $("#AccountGrid_pager").pager("reload"); });
        }
    }

    function _saveUserRoles(dlg) {
        var _roles = $("#UserRolesListBox").find(".d-listbox-values").val();
        if (_roles)
            $.post('@Url.Action("UpdateUserRoles", "Security", new { Area = "" })',
                    { username: _getSelectedUserName(), roles: _roles },
                    function () { dlg.dialog("close"); });
        else
            uiHelper.showMsg("@Html.Global("RES_MESSAGE")", "@Html.Global("ManageUser_RoleRequried")");
    }

    function _viewUserDetailHandler() {
        if (_ensureUserSelection()) {
            var userName = _getSelectedUserName();
            if (userName)
                $("#UserDetailViewContainer").load('@Url.Action("UserDetail", "Security", new { Area = "" })', { id: userName });
        }
    }

    function _getSelectedUserName() {
        var row = $("#AccountGrid").dgrid("option", "selectedRow");
        if (row) return $(row).find(".d-user-value").val();
        return "";
    }

</script>
@Html.StartupScripts()
